package main

import "fmt"

func main() {

	nums1 := []int{1, 2, 3, 0, 0, 0}
	nums2 := []int{2, 5, 6}
	marge(nums1, 3, nums2, 3)

	println(nums1)

	for j := 0; j < len(nums1); j++ {
		fmt.Printf("Element[%d] = %d\n", j, nums1[j])
	}
}

// 思路是，竞争插入数据数据
func marge(nums1 []int, m int, nums2 []int, n int) {

	tmp := make([]int, 0, m+n)
	p1, p2 := 0, 0
	for {
		if p1 == m {
			tmp = append(tmp, nums2[p2:]...)
			break
		}

		if p2 == n {
			tmp = append(tmp, nums1[p1:]...)
			break
		}

		if nums1[p1] < nums2[p2] {
			tmp = append(tmp, nums1[p1])
			p1++
			continue
		}

		tmp = append(tmp, nums2[p2])
		p2++
	}

	copy(nums1, tmp)

}
